package com.blog.dxxr.blogfront.Controller;

import com.blog.dxxr.blogfront.Common.Domain.ReturnObject;
import com.blog.dxxr.blogfront.Common.contants.Contants;
import com.blog.dxxr.blogfront.Common.utils.DateUtils;
import com.blog.dxxr.blogfront.Common.utils.UUIDUtils;
import com.blog.dxxr.blogfront.Domain.ThankComment;
import com.blog.dxxr.blogfront.Service.WebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ThankController {
    @Autowired
    private WebService webService;

    //异步查询网站访问量和评论数
    @RequestMapping("/index/queryWebAndCommentTotal.do")
    public @ResponseBody Object queryWebAndCommentTotal(){
        int webBrowseTotal = webService.selectWebBrowseTotal();
        int webCommentTotal = webService.queryCommentTotal();
        Map<String,Object> map = new HashMap<>();
        map.put("webBrowseTotal",webBrowseTotal);
        map.put("webCommentTotal",webCommentTotal);
        return map;
    }

    //异步实现鸣谢页面评论的提交
    @RequestMapping("/index/saveCommentThankPage.do")
    public @ResponseBody Object saveCommentThankPage(ThankComment thankComment){
        ReturnObject object = new ReturnObject();
        thankComment.setId(UUIDUtils.getUUID());
        thankComment.setCreateDate(DateUtils.formatDataTime(new Date()));
        try {
            int ret = webService.saveThankComment(thankComment);
            if (ret > 0){
                object.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
            }else {
                object.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
                object.setMessage("服务器繁忙，请稍后");
            }
        }catch (Exception e){
            e.printStackTrace();
            object.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            object.setMessage("服务器繁忙，请稍后");
        }
        return object;
    }

    //异步查询鸣谢页面评论
    @RequestMapping("/index/queryCommentThankPage.do")
    public @ResponseBody Object queryCommentThankPage(){
        List<ThankComment> thankCommentList = webService.queryThankComment();
        return thankCommentList;
    }
}
